﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using GTA;

namespace GTA
{
    public class Weapon
    {
        public Weapon(ContentManager Content)
        {
            this.Content = Content;
            texture = Content.Load<Texture2D>("Weapons/gun");
        }

        ContentManager Content;
        Texture2D texture;

        public enum WeaponType
        {
            Gun,
            ColdSteel, // Knife, firsts ...
            Explosive
        }

        public WeaponType Type
        {
            get { return type; }
            set { type = value; }
        }
        WeaponType type;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        string name = "none";

        public int Damage
        {
            get { return damage; }
            set { damage = value; }

        }
        int damage;

        public int FireRate
        {
            get { return fireRate; }
            set { fireRate = value; }

        }
        int fireRate = 7;

        public List<Bullet> BulletList
        {
            get { return bulletList; }
            set { bulletList = value; }
        }
        List<Bullet> bulletList = new List<Bullet>();

        int timer = 0;

        public void Update()
        {
            GunUpdate();
        }

        public void Draw(SpriteBatch spriteBatch, Vector2 playerPos, float angle)
        {
            spriteBatch.Draw(texture, playerPos, null, Color.White, angle, new Vector2(texture.Width / 2, texture.Height / 2), 1f, SpriteEffects.None, 0);

            foreach (Bullet item in bulletList)
                item.Draw(spriteBatch);

            spriteBatch.DrawString(Game1.font, "bulletNumber : " + bulletList.Count, new Vector2(0, 450), Color.Black);
        }

        /* Custom functions */
        private void GunUpdate()
        {
            if (type != WeaponType.Gun)
                return;

            for (int i = 0; i < bulletList.Count; i++)
            {
                bulletList[i].Update();
                if (bulletList[i].ToRemove)
                    bulletList.Remove(bulletList[i]);
            }
        }

        public void CreatBullet(Vector2 position, double angle)
        {
            timer++;

            if (Game1.State == Game1.GameState.Ingame && timer >= fireRate && Keyboard.GetState().IsKeyDown(Player.keyShoot))
            {
                Bullet temp = new Bullet(Content, position, angle);
                bulletList.Add(temp);
                timer = 0;
            }
        }


    }
}
